<?php
/**
 * @author 段志岩
 * @date 2008.8.17
 * @summary MVC中的C，控制类的基类。
 */

abstract class Action extends Base{
	protected $get;
	protected $post;
	protected $files;
	protected $smarty;
	protected $session;
	protected $db;

	abstract protected function init();

	abstract public function display();
	
	public function __construct()
	{
		global $gSmarty;
		global $db;

		$this->get = &$_GET;
		$this->post = &$_POST;
		$this->files = &$_FILES;
		$this->smarty = &$gSmarty;
		$this->session = &Session::getSession();
		$this->db = &$db;

		$this->smarty->assign("db", $this->db);
	}
	/**
	 * 根据签名检查变量，并给出错误提示信息。
	 *
	 * @see sanitize_vars()
	 */
	public function checkVars(& $vars, $sigs)
	{
		$failureKey = sanitize_vars($vars, $sigs);
		if($failureKey != "")
		{
			$this->showMessage("字段<strong>". $sigs[$failureKey]['name'] . "</strong>填写错误！");
		}
	}
	public function showMessage($msg)
	{
		$this->smarty->assign("errorMsg", $msg);
		$this->smarty->display("error.tpl");
		exit;
	}
	static public function forward($actionName, $actionMethod="")
	{
		$action = get_action_instance($actionName);
		if($actionMethod != "")
		{
			if(method_exists($action, $actionMethod)){
				$action->$actionMethod();
			}else{
				E("page_not_found");
			}
		}else{
			$action->doView();
		}
		exit;
	}
	static public function redirect($actionName, $actionMethod)
	{
		Action::forward($actionName, $actionMethod);
		exit;
	}
	public function redirectUrl($url)
	{
		header("Location: $url");
	}
};
?>
